package cn.yer.util;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class ConnectionFactory {

    private static DruidDataSource dataSource = null;

    static {
        Properties properties = new Properties();


        try {
            ClassLoader cl = ConnectionFactory.class.getClassLoader();
            InputStream is = cl.getResourceAsStream("jdbc.properties");
            properties.load(is);
            Class.forName(properties.getProperty("mysql.drivername", ""));
            dataSource = new DruidDataSource();
            dataSource.setUrl(properties.getProperty("mysql.url", ""));
            dataSource.setUsername(properties.getProperty("mysql.username", ""));
            dataSource.setPassword(properties.getProperty("mysql.password", ""));
            dataSource.setMinIdle(0);
            dataSource.setMaxPoolPreparedStatementPerConnectionSize(100);
            dataSource.setQueryTimeout(5000);
            dataSource.setName("pool_druid_api");


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * close resultSet
     *
     * @param rs
     */
    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * close Statement
     *
     * @param stmt
     */
    public static void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
                stmt = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * close PreparedStatement
     *
     */
    public static void closePreparedStatement(PreparedStatement pstmt) {
        if (pstmt != null) {
            try {
                pstmt.close();
                pstmt = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * close Connection
     *
     * @param conn
     */
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * close All
     */
    public static void closeAll(Object... args) {
        for (int i = 0; i < args.length; i++) {
            // close ResultSet
            if (args[i] instanceof ResultSet) {
                closeResultSet((ResultSet) args[i]);
                continue;
            }

            // close Statement
            if (args[i] instanceof Statement) {
                closeStatement((Statement) args[i]);
                continue;
            }

            // close PreparedStatement
            if (args[i] instanceof PreparedStatement) {
                closePreparedStatement((PreparedStatement) args[i]);
                continue;
            }

            // close Connection
            if (args[i] instanceof Connection) {
                closeConnection((Connection) args[i]);
                continue;
            }
        }
    }


    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        Connection connection = factory.getConnection();
        try {
            System.out.println(connection.getMetaData().getDriverName().toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
